package com.liuyuxing.test.controller;

import com.liuyuxing.test.Factory.PromotionStrategyFactory;
import com.liuyuxing.test.constants.PromotionType;
import com.liuyuxing.test.vo.FruitVO;
import com.liuyuxing.test.service.PromotionStrategyService;
import com.liuyuxing.test.service.ShoppingCartService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Log4j2
@RestController
@RequestMapping("/api/cart")
public class FruitController {

    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("/calculate")
    public double calculateTotal(@RequestBody FruitVO fruitVO) {
        //设置促销策略
        PromotionStrategyService promotionStrategy = PromotionStrategyFactory.getPromotionStrategy(PromotionType.valueOf(fruitVO.getPromotionType()));
        //按对应策略算出总价
        return shoppingCartService.calculateTotal(fruitVO.getFruit(),promotionStrategy);
    }
}
